.TH std::chrono::round(std::chrono::time_point) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::chrono::round(std::chrono::time_point) \- std::chrono::round(std::chrono::time_point)

.SH Synopsis
   Defined in header <chrono>
   template< class ToDuration, class Clock, class Duration >

   constexpr std::chrono::time_point<Clock, ToDuration>              \fI(since C++17)\fP

       round( const std::chrono::time_point<Clock, Duration>& tp );

   Returns the nearest time point to tp representable in ToDuration, rounding to even
   in halfway cases.

   The function does not participate in the overload resolution unless ToDuration is a
   specialization of std::chrono::duration and
   std::chrono::treat_as_floating_point_v<typename ToDuration::rep> is false.

.SH Parameters

   tp - time point to round to nearest

.SH Return value

   tp rounded to nearest time point using duration of type ToDuration, rounding to even
   in halfway cases.

.SH Possible implementation

   namespace detail
   {
       template<class> inline constexpr bool is_duration_v = false;
       template<class Rep, class Period> inline constexpr bool is_duration_v<
           std::chrono::duration<Rep, Period>> = true;
   }

   template<class To, class Clock, class FromDuration,
            class = std::enable_if_t<detail::is_duration_v<To> &&
                   !std::chrono::treat_as_floating_point_v<typename To::rep>>>
   constexpr std::chrono::time_point<Clock, To> round(
       const std::chrono::time_point<Clock, FromDuration>& tp)
   {
       return std::chrono::time_point<Clock, To>{
           std::chrono::round<To>(tp.time_since_epoch())};
   }

.SH Example



// Run this code

 #include <chrono>
 #include <iostream>
 #include <string>

 template<typename TimePoint>
 std::string to_string(const TimePoint& time_point)
 {
     return std::to_string(time_point.time_since_epoch().count());
 }

 int main()
 {
     using namespace std::literals::chrono_literals;
     using Sec = std::chrono::seconds;

     std::cout << "Time point\\t" "Cast\\t" "Floor\\t" "Round\\t" "Ceil\\n";
     std::cout << "(ms)\\t\\t"     "(s)\\t"  "(s)\\t"   "(s)\\t"   "(s)\\n";
     for (const auto value_ms : {5432ms, 5678ms})
     {
         std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>
             time_point_ms(value_ms);

         std::cout
             << to_string(time_point_ms) << "\\t\\t"
             << to_string(std::chrono::time_point_cast<Sec>(time_point_ms)) << '\\t'
             << to_string(std::chrono::floor<Sec>(time_point_ms)) << '\\t'
             << to_string(std::chrono::round<Sec>(time_point_ms)) << '\\t'
             << to_string(std::chrono::ceil<Sec>(time_point_ms)) << '\\n';
     }
 }

.SH Output:

 Time point      Cast    Floor   Round   Ceil
 (ms)            (s)     (s)     (s)     (s)
 5432            5       5       5       6
 5678            5       5       6       6

.SH See also

   time_point_cast                converts a time point to another time point on the
   \fI(C++11)\fP                        same clock, with a different duration
                                  \fI(function template)\fP
   ceil(std::chrono::time_point)  converts a time_point to another, rounding up
   \fI(C++17)\fP                        \fI(function template)\fP
   floor(std::chrono::time_point) converts a time_point to another, rounding down
   \fI(C++17)\fP                        \fI(function template)\fP
   round(std::chrono::duration)   converts a duration to another, rounding to nearest,
   \fI(C++17)\fP                        ties to even
                                  \fI(function template)\fP
   round
   roundf
   roundl
   lround
   lroundf
   lroundl
   llround
   llroundf                       nearest integer, rounding away from zero in halfway
   llroundl                       cases
   \fI(C++11)\fP                        \fI(function)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
